package com.huidz.calendar.mainfragment;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.haibin.calendarview.CalendarView;
import com.huidz.calendar.R;
import com.huidz.calendar.activity.AddActivity;
import com.huidz.calendar.activity.BeiWangLuActivity;
import com.huidz.calendar.activity.JiNianRiActivity;
import com.huidz.calendar.activity.JieRiActivity;
import com.huidz.calendar.activity.ShengRiActivity;
import com.huidz.calendar.model.BWLSModel;
import com.huidz.calendar.model.CalendarModel;
import com.huidz.calendar.util.Apiutils;
import com.huidz.calendar.util.Url;

import org.json.JSONObject;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RemindFragment extends Fragment implements
        CalendarView.OnYearChangeListener,
        View.OnClickListener, CalendarView.OnMonthChangeListener {
    //用于获取视图
    private View root;
    private CalendarView CalendarView;
    private ImageView hometitleadd;
    private LinearLayout bwl_look, sr_look, jnr_look, jr_look, bwl, srline, jnrline;

    private TextView titlecalendar;

    //3个背景textview
    private TextView bwl_bgtext, sr_bgtext, jnr_bgtext, bwl_text, sr_text, jnr_text;
    private RelativeLayout add_bwl, add_sr, add_jnr;

    private TextView jieri1, jieri1_text, jieri1_time, jieri1_day;
    private TextView jieri2, jieri2_text, jieri2_time, jieri2_day;
    private TextView jieri3, jieri3_text, jieri3_time, jieri3_day;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_tixing, container, false);//初始加载
        if (Apiutils.isNetworkConnecteds)
            InitView(root);
        return root;
    }

    public boolean loding = false;

    /**
     * 初始加载视图
     */
    public void InitView(View view) {
        jieri1 = view.findViewById(R.id.jieri1);
        jieri1_text = view.findViewById(R.id.jieri1_text);
        jieri1_time = view.findViewById(R.id.jieri1_time);
        jieri1_day = view.findViewById(R.id.jieri1_day);
        jieri2 = view.findViewById(R.id.jieri2);
        jieri2_text = view.findViewById(R.id.jieri2_text);
        jieri2_time = view.findViewById(R.id.jieri2_time);
        jieri2_day = view.findViewById(R.id.jieri2_day);
        jieri3 = view.findViewById(R.id.jieri3);
        jieri3_text = view.findViewById(R.id.jieri3_text);
        jieri3_time = view.findViewById(R.id.jieri3_time);
        jieri3_day = view.findViewById(R.id.jieri3_day);
        jr_look = view.findViewById(R.id.jr_look);
        jr_look.setOnClickListener(this);
        CalendarView = view.findViewById(R.id.calendarviews);
        titlecalendar = view.findViewById(R.id.titlecalendar);
        hometitleadd = view.findViewById(R.id.hometitleadd);
        hometitleadd.setOnClickListener(this);
        titlecalendar.setText(CalendarView.getCurYear() + "年" + CalendarView.getCurMonth() + "月");
        bwl_look = view.findViewById(R.id.bwl_look);
        bwl_look.setOnClickListener(this);
        sr_look = view.findViewById(R.id.sr_look);
        jnr_look = view.findViewById(R.id.jnr_look);
        sr_look.setOnClickListener(this);
        jnr_look.setOnClickListener(this);
        sr_bgtext = view.findViewById(R.id.sr_bgtext);
        jnr_bgtext = view.findViewById(R.id.jnr_bgtext);
        bwl_bgtext = view.findViewById(R.id.bwl_bgtext);
        bwl = view.findViewById(R.id.bwls);
        srline = view.findViewById(R.id.srs);
        jnrline = view.findViewById(R.id.jnrs);
        add_bwl = view.findViewById(R.id.add_bwl);
        add_bwl.setOnClickListener(this);
        add_sr = view.findViewById(R.id.add_sr);
        add_sr.setOnClickListener(this);
        add_jnr = view.findViewById(R.id.add_jnr);
        bwl_text = view.findViewById(R.id.bwl_text);
        sr_text = view.findViewById(R.id.sr_text);
        jnr_text = view.findViewById(R.id.jnr_text);
        bwl_text.setOnClickListener(this);
        sr_text.setOnClickListener(this);
        jnr_text.setOnClickListener(this);
        add_jnr.setOnClickListener(this);
        CalendarView.setOnMonthChangeListener(this);
        if (!Url.USER_TOKEN.equals(""))
            gettianqi();
        okHttp_postFromParameters();
        loding = true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.hometitleadd:
                Intent toaddbwls1 = new Intent(getActivity(), AddActivity.class);
                toaddbwls1.putExtra("index", "0");
                startActivity(toaddbwls1, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.bwl_text:
                if (bwlint > 0) {
                    Intent toaddbwls = new Intent(getActivity(), AddActivity.class);
                    toaddbwls.putExtra("index", "0");
                    startActivity(toaddbwls, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;

            case R.id.sr_text:
                if (sint > 0) {
                    Intent toaddsrs = new Intent(getActivity(), AddActivity.class);
                    toaddsrs.putExtra("index", "1");
                    startActivity(toaddsrs, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;

            case R.id.jnr_text:
                if (jnrint > 0) {
                    Intent toaddjnrs = new Intent(getActivity(), AddActivity.class);
                    toaddjnrs.putExtra("index", "2");
                    startActivity(toaddjnrs, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;
            case R.id.jr_look:
                Intent tojr = new Intent(getActivity(), JieRiActivity.class);
                startActivity(tojr, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.bwl_look:
                if (Url.USER_TOKEN.equals(""))
                    Apiutils.SetToast(getActivity(), "请先登录");
                else {
                    Intent tobwl = new Intent(getActivity(), BeiWangLuActivity.class);
                    startActivity(tobwl, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;
            case R.id.sr_look:
                if (Url.USER_TOKEN.equals(""))
                    Apiutils.SetToast(getActivity(), "请先登录");
                else {
                    Intent tosr = new Intent(getActivity(), ShengRiActivity.class);
                    startActivity(tosr, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;
            case R.id.jnr_look:
                if (Url.USER_TOKEN.equals(""))
                    Apiutils.SetToast(getActivity(), "请先登录");
                else {
                    Intent tojnr = new Intent(getActivity(), JiNianRiActivity.class);
                    startActivity(tojnr, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;
            case R.id.add_bwl:
                if (Url.USER_TOKEN.equals(""))
                    Apiutils.SetToast(getActivity(), "请先登录");
                else {
                    Intent toaddbwl = new Intent(getActivity(), AddActivity.class);
                    toaddbwl.putExtra("index", "0");
                    startActivity(toaddbwl, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;
            case R.id.add_sr:
                if (Url.USER_TOKEN.equals(""))
                    Apiutils.SetToast(getActivity(), "请先登录");
                else {
                    Intent toaddsr = new Intent(getActivity(), AddActivity.class);
                    toaddsr.putExtra("index", "1");
                    startActivity(toaddsr, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;
            case R.id.add_jnr:
                if (Url.USER_TOKEN.equals(""))
                    Apiutils.SetToast(getActivity(), "请先登录");
                else {
                    Intent toaddjnr = new Intent(getActivity(), AddActivity.class);
                    toaddjnr.putExtra("index", "2");
                    startActivity(toaddjnr, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                }
                break;
        }
    }

    @Override
    public void onMonthChange(int year, int month) {
        titlecalendar.setText(year + "年" + month + "月");
    }

    @Override
    public void onYearChange(int year) {

    }

    public static boolean shuaxing = false;

    @Override
    public void onResume() {
        super.onResume();
        if (Url.USER_TOKEN.equals("") && loding) {
            bwl_bgtext.setVisibility(View.VISIBLE);
            add_bwl.setVisibility(View.VISIBLE);
            sr_bgtext.setVisibility(View.VISIBLE);
            add_sr.setVisibility(View.VISIBLE);
            jnr_bgtext.setVisibility(View.VISIBLE);
            add_jnr.setVisibility(View.VISIBLE);
            bwl.setVisibility(View.GONE);
            srline.setVisibility(View.GONE);
            jnrline.setVisibility(View.GONE);
        }
        if (!Url.USER_TOKEN.equals("") && shuaxing) {
            shuaxing = false;
            bwl.removeAllViews();
            srline.removeAllViews();
            jnrline.removeAllViews();
            gettianqi();
        }
    }

    String time_s;

    public void gettianqi() {
        OkHttpClient client = new OkHttpClient();
        //构造Request对象
        //采用建造者模式，链式调用指明进行Get请求,传入Get的请求地址
        Request request = new Request.Builder().get().url(Url.GETBWL + "?logintoken=" + Url.USER_TOKEN).build();
        Call call = client.newCall(request);
        //异步调用并设置回调函数
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, final Response response) {
                try {
                    final String responseStr;
                    responseStr = response.body().string();
                    Message message = new Message();
                    message.obj = responseStr;
                    gettqs.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public int bwlint = 0, sint = 0, jnrint = 0;
    public ArrayList<BWLSModel.data> new_list_views = new ArrayList<>();
    public static BWLSModel userms1;
    public Handler gettqs = new Handler() {
        public void handleMessage(Message msg) {
            String msgs = null;
            try {
                JSONObject success_Object = null;
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    msgs = success_Object.getString("msg");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        userms1 = gson.fromJson(msg.obj.toString(),
                                BWLSModel.class);
                        new_list_views.addAll(userms1.getData());
                        for (int is = 0; is < new_list_views.size(); is++) {
                            if (new_list_views.get(is).getType().equals("1"))
                                bwlint++;
                            if (new_list_views.get(is).getType().equals("2"))
                                sint++;
                            if (new_list_views.get(is).getType().equals("3"))
                                jnrint++;
                        }
                        if (bwlint > 0) {
                            bwl_bgtext.setVisibility(View.GONE);
                            add_bwl.setVisibility(View.GONE);
                            bwl.setBackgroundColor(getActivity().getResources().getColor(R.color.view));
                            for (int i = 0; i < new_list_views.size(); i++) {
                                if (new_list_views.get(i).getType().equals("1")) {
                                    LinearLayout relativeLayout = new LinearLayout(getActivity());
                                    TextView textView = new TextView(getActivity());
                                    TextView textViews = new TextView(getActivity());
                                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout.LayoutParams lp1s = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    lp1.setMargins(30, 24, 0, 24);
                                    lp1s.setMargins(30, 24, 0, 24);
                                    lp.setMargins(0, 2, 0, 0);
                                    relativeLayout.setLayoutParams(lp);
                                    relativeLayout.setOrientation(LinearLayout.HORIZONTAL);
                                    relativeLayout.setBackgroundColor(getActivity().getResources().getColor(R.color.colorwhite));
                                    textView.setLayoutParams(lp1);
                                    long sjc = Long.valueOf(new_list_views.get(i).getRemindtime());
                                    textView.setText(getDateToString(sjc));
                                    textView.setTextColor(getActivity().getResources().getColor(R.color.title_color));
                                    textView.setTextSize(12);
                                    textView.setGravity(Gravity.CENTER);
                                    textViews.setLayoutParams(lp1s);
                                    textViews.setText(new_list_views.get(i).getMemcontent());
                                    textViews.setTextColor(getActivity().getResources().getColor(R.color.text_msg));
                                    textViews.setTextSize(12);
                                    textViews.setGravity(Gravity.CENTER);
                                    relativeLayout.addView(textView);
                                    relativeLayout.addView(textViews);
                                    bwl.addView(relativeLayout);
                                }
                                if (new_list_views.size() - i == 1) {
                                    RelativeLayout relativeLayout = new RelativeLayout(getActivity());
                                    RelativeLayout relativeLayouts = new RelativeLayout(getActivity());
                                    TextView textViews = new TextView(getActivity());
                                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    RelativeLayout.LayoutParams lp1s = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    lp1s.setMargins(30, 24, 30, 24);
                                    lp1.setMargins(0, 2, 0, 0);
                                    lp.setMargins(0, 10, 0, 10);
                                    lp1s.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                                    lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                                    relativeLayouts.setLayoutParams(lp1);
                                    relativeLayouts.setBackgroundColor(getActivity().getResources().getColor(R.color.colorwhite));
                                    relativeLayout.setLayoutParams(lp);
                                    relativeLayout.setBackground(getActivity().getResources().getDrawable(R.drawable.hl_list_red));
                                    textViews.setLayoutParams(lp1s);
                                    textViews.setText("+ 继续添加");
                                    textViews.setTextColor(getActivity().getResources().getColor(R.color.title_color));
                                    textViews.setTextSize(12);
                                    textViews.setGravity(Gravity.CENTER);
                                    relativeLayout.addView(textViews);
                                    relativeLayouts.addView(relativeLayout);
                                    bwl.addView(relativeLayouts);
                                    relativeLayout.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Intent toaddbwl = new Intent(getActivity(), AddActivity.class);
                                            toaddbwl.putExtra("index", "0");
                                            startActivity(toaddbwl, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                                        }
                                    });
                                }
                            }
                        } else {
                            bwl_bgtext.setVisibility(View.VISIBLE);
                            add_bwl.setVisibility(View.VISIBLE);
                        }
                        if (sint > 0) {
                            sr_bgtext.setVisibility(View.GONE);
                            add_sr.setVisibility(View.GONE);
                            srline.setBackgroundColor(getActivity().getResources().getColor(R.color.view));
                            for (int i = 0; i < new_list_views.size(); i++) {
                                if (new_list_views.get(i).getType().equals("2")) {
                                    LinearLayout relativeLayout = new LinearLayout(getActivity());
                                    TextView textView = new TextView(getActivity());
                                    TextView textViews = new TextView(getActivity());
                                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout.LayoutParams lp1s = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    lp1.setMargins(30, 24, 0, 24);
                                    lp1s.setMargins(30, 24, 0, 24);
                                    lp.setMargins(0, 2, 0, 0);
                                    relativeLayout.setLayoutParams(lp);
                                    relativeLayout.setOrientation(LinearLayout.HORIZONTAL);
                                    relativeLayout.setBackgroundColor(getActivity().getResources().getColor(R.color.colorwhite));
                                    textView.setLayoutParams(lp1);
                                    long sjc = Long.valueOf(new_list_views.get(i).getRemindtime());
                                    textView.setText(getDateToString(sjc));
                                    textView.setTextColor(getActivity().getResources().getColor(R.color.title_color));
                                    textView.setTextSize(12);
                                    textView.setGravity(Gravity.CENTER);
                                    textViews.setLayoutParams(lp1s);
                                    textViews.setText(new_list_views.get(i).getBirthdayperson());
                                    textViews.setTextColor(getActivity().getResources().getColor(R.color.text_msg));
                                    textViews.setTextSize(12);
                                    textViews.setGravity(Gravity.CENTER);
                                    relativeLayout.addView(textView);
                                    relativeLayout.addView(textViews);
                                    srline.addView(relativeLayout);
                                }
                                if (new_list_views.size() - i == 1) {
                                    RelativeLayout relativeLayout = new RelativeLayout(getActivity());
                                    RelativeLayout relativeLayouts = new RelativeLayout(getActivity());
                                    TextView textViews = new TextView(getActivity());
                                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    RelativeLayout.LayoutParams lp1s = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    lp1s.setMargins(30, 24, 30, 24);
                                    lp1.setMargins(0, 2, 0, 0);
                                    lp.setMargins(0, 10, 0, 10);
                                    lp1s.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                                    lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                                    relativeLayouts.setLayoutParams(lp1);
                                    relativeLayouts.setBackgroundColor(getActivity().getResources().getColor(R.color.colorwhite));
                                    relativeLayout.setLayoutParams(lp);
                                    relativeLayout.setBackground(getActivity().getResources().getDrawable(R.drawable.hl_list_red));
                                    textViews.setLayoutParams(lp1s);
                                    textViews.setText("+ 继续添加");
                                    textViews.setTextColor(getActivity().getResources().getColor(R.color.title_color));
                                    textViews.setTextSize(12);
                                    textViews.setGravity(Gravity.CENTER);
                                    relativeLayout.addView(textViews);
                                    relativeLayouts.addView(relativeLayout);
                                    srline.addView(relativeLayouts);
                                    relativeLayout.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Intent toaddbwl = new Intent(getActivity(), AddActivity.class);
                                            toaddbwl.putExtra("index", "1");
                                            startActivity(toaddbwl, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                                        }
                                    });
                                }
                            }
                        } else {
                            sr_bgtext.setVisibility(View.VISIBLE);
                            add_sr.setVisibility(View.VISIBLE);
                        }
                        if (jnrint > 0) {
                            jnr_bgtext.setVisibility(View.GONE);
                            add_jnr.setVisibility(View.GONE);
                            jnrline.setBackgroundColor(getActivity().getResources().getColor(R.color.view));
                            for (int i = 0; i < new_list_views.size(); i++) {
                                if (new_list_views.get(i).getType().equals("3")) {
                                    LinearLayout relativeLayout = new LinearLayout(getActivity());
                                    TextView textView = new TextView(getActivity());
                                    TextView textViews = new TextView(getActivity());
                                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout.LayoutParams lp1s = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    lp1.setMargins(40, 34, 0, 34);
                                    lp1s.setMargins(40, 34, 0, 34);
                                    lp.setMargins(0, 2, 0, 0);
                                    relativeLayout.setLayoutParams(lp);
                                    relativeLayout.setOrientation(LinearLayout.HORIZONTAL);
                                    relativeLayout.setBackgroundColor(getActivity().getResources().getColor(R.color.colorwhite));
                                    textView.setLayoutParams(lp1);
                                    long sjc = Long.valueOf(new_list_views.get(i).getRemindtime());
                                    textView.setText(getDateToString(sjc));
                                    textView.setTextColor(getActivity().getResources().getColor(R.color.title_color));
                                    textView.setTextSize(12);
                                    textView.setGravity(Gravity.CENTER);
                                    textViews.setLayoutParams(lp1s);
                                    textViews.setText(new_list_views.get(i).getCommem());
                                    textViews.setTextColor(getActivity().getResources().getColor(R.color.text_msg));
                                    textViews.setTextSize(12);
                                    textViews.setGravity(Gravity.CENTER);
                                    relativeLayout.addView(textView);
                                    relativeLayout.addView(textViews);
                                    jnrline.addView(relativeLayout);
                                }
                                if (new_list_views.size() - i == 1) {
                                    RelativeLayout relativeLayout = new RelativeLayout(getActivity());
                                    RelativeLayout relativeLayouts = new RelativeLayout(getActivity());
                                    TextView textViews = new TextView(getActivity());
                                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    RelativeLayout.LayoutParams lp1s = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    lp1s.setMargins(30, 24, 30, 24);
                                    lp1.setMargins(0, 2, 0, 0);
                                    lp.setMargins(0, 10, 0, 10);
                                    lp1s.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                                    lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                                    relativeLayouts.setLayoutParams(lp1);
                                    relativeLayouts.setBackgroundColor(getActivity().getResources().getColor(R.color.colorwhite));
                                    relativeLayout.setLayoutParams(lp);
                                    relativeLayout.setBackground(getActivity().getResources().getDrawable(R.drawable.hl_list_red));
                                    textViews.setLayoutParams(lp1s);
                                    textViews.setText("+ 继续添加");
                                    textViews.setTextColor(getActivity().getResources().getColor(R.color.title_color));
                                    textViews.setTextSize(12);
                                    textViews.setGravity(Gravity.CENTER);
                                    relativeLayout.addView(textViews);
                                    relativeLayouts.addView(relativeLayout);
                                    jnrline.addView(relativeLayouts);
                                    relativeLayout.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Intent toaddbwl = new Intent(getActivity(), AddActivity.class);
                                            toaddbwl.putExtra("index", "2");
                                            startActivity(toaddbwl, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                                        }
                                    });
                                }
                            }
                        } else {
                            jnr_bgtext.setVisibility(View.VISIBLE);
                            add_jnr.setVisibility(View.VISIBLE);
                        }
                    } else {
                        Apiutils.SetToast(getActivity(), msgs + "");
                    }
                }

            } catch (
                    Exception e) {
                e.printStackTrace();
            }

        }

        public String getDateToString(long milSecond) {
            Date date = new Date(milSecond * 1000);
            SimpleDateFormat format = new SimpleDateFormat("MM-dd");
            return format.format(date);
        }
    };

    public void okHttp_postFromParameters() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    RequestBody formBody = new FormBody.Builder().add("year", 2020 + "")
                            .build();
                    Request request = new Request.Builder().url(Url.JIARI).post(formBody).build();
                    okhttp3.Response response = okHttpClient.newCall(request).execute();
                    String msg = response.body().string();
                    Message message = new Message();
                    message.obj = msg;
                    gethot.sendMessage(message);
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public boolean listret = false;
    ArrayList<String> time = new ArrayList<>();
    ArrayList<String> name = new ArrayList<>();
    public CalendarModel userm;
    public Handler gethot = new Handler() {
        public void handleMessage(Message msg) {
            String msgs = null;
            try {
                JSONObject success_Object = null;
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    msgs = success_Object.getString("msg");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        userm = gson.fromJson(msg.obj.toString(),
                                CalendarModel.class);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //创建对象制定日期格式
                        String dates = format.format(new Date());
                        for (int s = 0; s < userm.getData().size(); s++) {
                            if (dateDiff(dates, userm.getData().get(s).getDate()) > 0) {
                                if (name.size() > 0 && userm.getData().get(s).getName().equals(name.get(name.size() - 1))) {

                                } else {
                                    time.add(userm.getData().get(s).getDate());
                                    name.add(userm.getData().get(s).getName());
                                }
                            }
                        }
                        for (int siz = 0; siz < name.size(); siz++) {
                            Apiutils.SetLog(name.get(siz));
                        }
                        jieri1.setText(name.get(0));
                        jieri1_time.setText(time.get(0));
                        jieri1_day.setText(dateDiff(dates, time.get(0))+"天");
                        jieri1_text.setText("距离");
                        jieri2.setText(name.get(1));
                        jieri2_time.setText(time.get(1));
                        jieri2_day.setText(dateDiff(dates, time.get(1))+"天");
                        jieri2_text.setText("距离");
                        jieri3.setText(name.get(2));
                        jieri3_time.setText(time.get(2));
                        jieri3_day.setText(dateDiff(dates, time.get(2))+"天");
                        jieri3_text.setText("距离");
                    } else {
                        Apiutils.SetToast(getActivity(), msgs + "");
                    }
                }
            } catch (
                    Exception e) {
                e.printStackTrace();
            }
        }
    };

    public long dateDiff(String startTime, String endTime) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            if (day >= 1) {
                return day;
            } else {
                if (day == 0) {
                    return 1;
                } else {
                    return 0;
                }

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }
}